package com.tuni.birthdaymoney.persistence.entity;

import static javax.persistence.GenerationType.IDENTITY;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name = "BM_Message")
public class Message {
    public enum EmailType {
        // DO NOT CHANGE THE ORDER
        NOTIFICATION, CONGRATULATION
    }

    private Integer id;
    private EmailType type;
    private Person birthdayMan;
    private int birthdayYear;

    public Message() {
    }

    public Message(Person birthdayMan, EmailType type, int birthdayYear) {
        this.birthdayMan = birthdayMan;
        this.type = type;
        this.birthdayYear = birthdayYear;
    }

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "ID", unique = true, nullable = false)
    public Integer getID() {
        return this.id;
    }

    public void setID(Integer id) {
        this.id = id;
    }

    @Column(name = "EmailType", nullable = false)
    @Enumerated(EnumType.ORDINAL)
    public EmailType getType() {
        return this.type;
    }

    public void setType(EmailType type) {
        this.type = type;
    }

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "BirthdayManID", nullable = false)
    public Person getBirthdayMan() {
        return this.birthdayMan;
    }

    public void setBirthdayMan(Person birthdayMan) {
        this.birthdayMan = birthdayMan;
    }

    @Column(name = "BirthdayYear", nullable = false)
    public int getBirthdayYear() {
        return this.birthdayYear;
    }

    public void setBirthdayYear(int birthdayYear) {
        this.birthdayYear = birthdayYear;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + birthdayMan.getId();
        result = prime * result + birthdayYear;
        result = prime * result + ((type == null) ? 0 : type.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Message other = (Message) obj;
        if (birthdayMan.getId() != other.birthdayMan.getId())
            return false;
        if (birthdayYear != other.birthdayYear)
            return false;
        if (type != other.type)
            return false;
        return true;
    }
}